// 获取详情页地址
// console.log('location.href  ',location.href)
let id = location.href.split('?').splice(-1, 1)
// console.log(id);
let httpStr = 'http://www.xiongmaoyouxuan.com/api/detail?' + id + '&normal=1&sa=&ignore_source_pid='
// console.log(httpStr)
// 渲染图片
$.get(httpStr, function (result) {
    // 获取图片渲染数据
    let singleDetails = result.data.detail.descContentList
    let productDetails = result.data.detail.photo
    let purchasePrice = result.data.detail.price
    let purchaseName = result.data.detail.title
    // console.log(productDetails);
    let data1 = { productDetails }
    let data = { singleDetails }
    // 渲染商品名
    $('.purchaseName').html(purchaseName)
    // 渲染价格
    $('.purchasePrice>span').html(purchasePrice)
    // 渲染放大镜图片
    $('.img').prop('src', productDetails[0].url)
    $('.bigImg').prop('src', productDetails[0].url)
    // 渲染详情图
    $('#productDetails').html(template('productDetails', data1))
    // 渲染下面更多图
    $('.morePicture').html(template("morePicture", data))
})
// 放大镜小图点击事件
function glass(index) {
    $.get(httpStr, function (result) {
        let productDetails = result.data.detail.photo
        $('.img').prop('src', productDetails[index].url)
        $('.bigImg').prop('src', productDetails[index].url)
    })
}
// 松开状态
$('.picture1').on('touchend', function () {
    $('.smallGlass').css('display', 'none')
    $('.bigGlass').css('display', 'none')
})
// 放大镜移动
$('.picture1').on('touchmove', function (e) {
    e.preventDefault();
    let touch = e.originalEvent.targetTouches[0]
    let y = touch.pageY - 56;
    let x = touch.pageX - 80;
    if (x < 0) { x = 0 }
    if (y < 0) { y = 0 }
    if (x > 90) { x = 90 }
    if (y > 90) { y = 90 }
    $('.smallGlass').css('display', 'block')
    $('.bigGlass').css('display', 'block')
    $('.smallGlass').css('left', x)
    $('.bigImg').css('left', -2 * x)
    $('.smallGlass').css('top', y)
    $('.bigImg').css('top', -2 * y)
})
// 尺寸选择
$('.purchaseSize>div').on('click', function () {
    $(this).css('background', 'blue').siblings().css('background', 'white')
})
// 颜色选择
$('.purchaseColor>div').on('click', function () {
    $(this).css('background', 'blue').siblings().css('background', 'white')
})
// 数量改变
$('.minus').on('click', function () {
    let a = $('.num1').val()
    a--
    if (a <= 0) {
        a = 0
    }
    $('.num1').val(a)
})
$('.plus').on('click', function () {
    let b = $('.num1').val()
    b++
    $('.num1').val(b)
})
// 加入购物车,需要一下数据
// 单价，数量，颜色，大小，名字，图片
$('.shopCart').on('click', function () {
    let shopCartListstr = localStorage.getItem('LIST')
    let shopCartList = JSON.parse(shopCartListstr) || []
    // console.log(shopCartList)
    $.get(httpStr, function (result) {
        // 单价
        let purchasePrice = result.data.detail.price
        // 名字
        let purchaseName = result.data.detail.title
        // 图片
        let productDetails = result.data.detail.photo[0].url
        // 数量
        let num=$('.num1').val()
        // 颜色
        let size=$('.purchaseSize>div[style="background: blue;"]').html()
        // 大小
        let color=$('.purchaseColor>div[style="background: blue;"]').html()
        // 编号
        let productId=id
        // 单选
        let state=false
        
        let productInformation={state,productId,purchasePrice,purchaseName,productDetails,num,size,color}
      
        // 去重
        let item=shopCartList.find(item=>item.productId[0]==productInformation.productId[0]&&item.color==productInformation.color&&item.size==productInformation.size)
        console.log(item);
        if(item){
            // console.log(7676);
            item.num=parseInt(productInformation.num)+parseInt(item.num)
        }else{
            // console.log(66)
            shopCartList.push(productInformation)
        }
        localStorage.setItem('LIST',JSON.stringify(shopCartList))
    })

})
// 返回首页
$('.back').on('click',function(){
    location.href='index.html'
})
// 收藏
$('.collect').on('click',function(){
    console.log($('.circle').css('background-color'))
    if($('.circle').css('background-color')=='rgb(253, 222, 74)'){
        console.log(77)
        $('.circle').css('background-color','rgb(255, 0, 0)')
    }else{
        console.log(556)
        $('.circle').css('background-color','rgb(253, 222, 74)')
    }
})
// 跳转到购物车页面
$('.skip').on('click',function(){
    location.href='shopCart.html'
})

let timer = null;
box.onclick = function () {
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
            document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
            timer = requestAnimationFrame(fn);
        } else {
            cancelAnimationFrame(timer);
        }
    });
}